//
//  SettingObject.m
//  
//
//  Created by Apple on 15/8/12.
//  Copyright (c) 2015年 Apple. All rights reserved.
//

#import "SettingObject.h"
#include <sys/param.h>
#include <sys/mount.h>



@interface SettingObject ()


@end

@implementation SettingObject

#define NUMBER_REGEX @"[^0-9]"

//字典转换成json格式字符串
+ (NSString*)dictionaryToJson:(NSDictionary *)dic

{
    
    NSError *parseError = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
}


//获取字符串宽高

+(CGSize)sizeWithContenString:(NSString*)string font:(UIFont*)font size:(CGSize)size{

    NSDictionary *attributes = @{NSFontAttributeName:font};
    CGRect summaryRect = [string boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
    return summaryRect.size;

}


//弹框
+(void)showAlert:(NSString *)title content:(NSString *)content{
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:title message:content delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

//检查Object是否有效
+ (BOOL)objectIsValid:(id)object{
    if([NSNull null] != object && nil != object && [object isKindOfClass:[NSString class]] && ((NSString*)object).length > 0){
        return YES;
    }else if([NSNull null] != object && nil != object && [object isKindOfClass:[NSArray class]] && [(NSArray*)object count] > 0){
        return YES;
    }else if([NSNull null] != object && nil != object && [object isKindOfClass:[NSMutableArray class]] && [(NSMutableArray*)object count] > 0){
        return YES;
    }else if([NSNull null] != object && nil != object && [object isKindOfClass:[NSDictionary class]]){
        return YES;
    }else if([NSNull null] != object && nil != object && [object isKindOfClass:[NSMutableDictionary class]]){
        return YES;
    }else if ([NSNull null] != object && nil != object && [object isKindOfClass:[NSNumber class]]) {
        return YES;
    } else {
        return NO;
    }
}
+ (NSAttributedString *)labelLoadHTMLWithString:(NSString *)titleString{

    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[titleString dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
    return attrStr;
}
+ (UIViewController *)getCurrentViewController
{
    UIViewController *result = nil;
    //获取当前显示的vc
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    return result;
}

+ (UIViewController *)topViewController {
    UIViewController *resultVC;
    resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
    while (resultVC.presentedViewController) {
        resultVC = [self _topViewController:resultVC.presentedViewController];
    }
    return resultVC;
}

+ (UIViewController *)_topViewController:(UIViewController *)vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self _topViewController:[(UINavigationController *)vc topViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
    } else {
        return vc;
    }
    return nil;
}


//检查是否为手机号
+(BOOL)checkStringIsPhoneNumber:(NSString*)str{
    
    BOOL allNumber = NO;
    BOOL length = NO;
    BOOL prefix = NO;
    if ([SettingObject isAllNumber:str]) {
        allNumber = YES;
    }
    if ([str length] == 11) {
        length = YES;
    }
    if ([str hasPrefix:@"13"]||[str hasPrefix:@"15"]||[str hasPrefix:@"18"]||[str hasPrefix:@"14"]||[str hasPrefix:@"17"]) {
        prefix = YES;
    }
    
    if (allNumber && length && prefix) {
        return YES;
    } else {
        return  NO;
    }
}
//检查是否都是数字
+(BOOL)isAllNumber:(NSString *)contentStr{
    if (!contentStr) {
        return NO;
    }
    NSError *error;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:NUMBER_REGEX options:0 error:&error];
    
    NSArray *matches = [regex matchesInString:contentStr options:0 range:NSMakeRange(0, [contentStr length])];
    
    NSMutableString *content_sb = [[NSMutableString alloc]init];
    for (NSTextCheckingResult *match in matches) {
        NSRange matchRange = [match range];
        [content_sb appendString:[contentStr substringWithRange:matchRange]];
    }
    return content_sb.length==0?YES:NO;
}


+ (void)pushviewController:(UIViewController *)pushVC{
    UIViewController * vc = [SettingObject topViewController];
    pushVC.hidesBottomBarWhenPushed = YES;
    [vc.navigationController pushViewController:pushVC animated:YES];
}
//获取总内存大小
+ (long long)getTotalMemorySize
{
    return [NSProcessInfo processInfo].physicalMemory;
}
//获取剩余存储空间
+ (NSString *) freeDiskSpaceInBytes{
    struct statfs buf;
    long long freespace = -1;
    if(statfs("/var", &buf) >= 0){
        freespace = (long long)(buf.f_bsize * buf.f_bfree);
    }
    return [NSString stringWithFormat:@"剩余：%qi MB" ,freespace/1024/1024];
}
@end











